package club.ruanx.movie.service.impl;

import club.ruanx.bean.PageBean;
import club.ruanx.core.service.base.BaseServiceImpl;
import club.ruanx.movie.entity.Person;
import club.ruanx.movie.param.PersonParam;
import club.ruanx.movie.repository.PersonRepository;
import club.ruanx.movie.service.PersonService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.stream.Stream;

/**
 * @author 阮胜
 * @date 2018/10/22 14:51
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PersonServiceImpl extends BaseServiceImpl<Person, String, PersonRepository> implements PersonService {
    @Override
    public Person save(PersonParam personParam) {
        Integer[] professions = personParam.getProfessions();
        if (professions.length == 0) {
            professions = Stream.of(1).toArray(value -> new Integer[1]);
        }
        Person person = new Person();
        BeanUtils.copyProperties(personParam, person);
        person.setProfessions(JSON.toJSONString(professions));
        return repository.save(person);
    }

    @Override
    public Page<Person> findByProfession(Integer profession, PageBean pageBean) {
        return repository.findByProfession(profession, pageBean.toPageable());
    }

    @Override
    public boolean existsBySpiderId(String spiderId) {
        return repository.existsBySpiderId(spiderId);
    }

    @Override
    public Page<Person> findAllWherePhotosNotEmpty(PageBean pageBean) {
        return repository.findAllWherePhotosNotEmpty(pageBean.toPageable());
    }
}
